package projorientado.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.Iterator;
import javax.imageio.ImageIO;
import projorientado.ocr.OCRProvider;
import projorientado.spi.GenericServiceProviderInterface;
import projorientado.spi.ServiceProvider;
import projorientado.util.CodeTokenizer;
import projorientado.util.CodeTokenizer.CodeElement;
import projorientado.util.CodeTokenizer.CodeLine;
import projorientado.util.CodeTokenizer.CodeVarDef;

public class ImageLayerDataProvider extends GenericServiceProviderInterface<ImageLayerData> {

    private static final ImageLayerDataProvider instance = new ImageLayerDataProvider();

    static {
        instance.registerServiceProvider("BrightnessFileImage", new BrightnessFileImage());
    }

    private ImageLayerDataProvider() {

    }

    public static ImageLayerDataProvider getInstance() {
        return instance;
    }

    public static ImageLayerData getImageLayerData(String s) {
        return instance.getService(s);
    }

    public static ImageLayerData getImageLayerData(String name, String param) {
        return instance.getService(name, param);
    }

    private static class BrightnessFileImage implements ServiceProvider<ImageLayerData> {

        public ImageLayerData getService(String s) {

            try {
                URL url = null;
                String process = null;

                CodeTokenizer.CodeBlock root = CodeTokenizer.singleParse(s);

                Iterator<CodeTokenizer.CodeElement> it = root.childs.iterator();
                while (it.hasNext()) {
                    CodeElement e = it.next();
                    if (e instanceof CodeTokenizer.CodeTextBlock) {
                        CodeTokenizer.CodeTextBlock tblock = (CodeTokenizer.CodeTextBlock) e;
                        String name = tblock.name.toLowerCase();

                        if (name.startsWith("process") || name.startsWith("processor")) {
                            process = tblock.text;
                        }
                    } else if (e instanceof CodeTokenizer.CodeLine) {
                        CodeVarDef var = CodeTokenizer.parseVarDef(((CodeLine) e).line);
                        if (var.varName.equalsIgnoreCase("file")) {
                            url = new File(var.value).toURI().toURL();
                        } else if (var.varName.equalsIgnoreCase("url")) {
                            url = new URL(var.value);
                        }
                    }
                }

                BufferedImage image = ImageIO.read(url);
                
                if(image == null)
                    return null;
                
                ImageLayerData ild = ImageLayerData.extractBrightnessLayer(image);
                
                if(process != null) {
                    ild = ild.process(process);
                }
                
                return ild;
            } catch (Exception e) {
                return null;
            }
        }
    }
}
